package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.web.HttpGameWebParam;
import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.server.manger.NameMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseGameHttpServlet;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/reName")
public class PlayerRename extends BaseGameHttpServlet {

    private static final long serialVersionUID = 1L;

    private static Logger logger = LogManager.getLogger(PlayerRename.class.getName());

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpGameWebParam httpGameWebParam = super.parseRequest(request);
        JSONObject paramObj = httpGameWebParam.getParamObj();
        String param = httpGameWebParam.getParam();
        String language = httpGameWebParam.getLanguage();

        String nickName = null;
        long userId = 0;
        if (paramObj.getString("nickName") != null) {
            nickName = paramObj.getString("nickName");
        }
        if (paramObj.getString("userId") != null) {
            userId = Long.parseLong(paramObj.getString("userId"));
        }
        int result = checkNickName(nickName);

        if (result != 0) {
            sendMessage(response, getResultObj(result, language).toJSONString());
            logger.info("nickName Error. userId: {}, nickName: {}, result: {}", userId, nickName, request);
        } else {
            if (UserBussiness.updateNickName(userId, nickName)) {
                sendMessage(response, getResultObj(0, language).toJSONString());
            } else {
                sendMessage(response, getResultObj(GameErrorCode.E_CHANGE_NICKNAME_SAVE_DB_ERROR, language).toJSONString());
            }
        }
    }

    private int checkNickName(String nickName) {
        int length = StringUtils.getStringLen(nickName);
        int channelId = ServerListMgr.getChannelId(WebServer.getInstance().getServerId());
        if (length > NameMgr.getMaxNickNameLength(channelId) || length < NameMgr.MIN_NICK_NAME_LENGTH) {
            return GameErrorCode.E_NAME_LENGTH_OVER;
        }
        if (UserBussiness.isSameNickName(nickName)) {
            return GameErrorCode.E_NICK_NAME_SAME;
        }
        if (GameWebServiceInvoke.checkNameForbidden(nickName)) {
            return GameErrorCode.E_FORBIDDEN_WORD;
        }
        return 0;
    }
}
